//package cn.hxzy.filter;
//
//
//import cn.hxzy.domain.User;
//import cn.hxzy.util.JwtUtils;
//import cn.hxzy.util.ThreadLocalUtil;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import io.jsonwebtoken.Claims;
//import io.netty.util.internal.StringUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.util.ObjectUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//
////每一个servlet请求，只会执行一次
//@Component
//public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
//
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//            //获取当前请求的url地址
//            String url = request.getRequestURI();
//            //如果当前请求不是登录请求，则需要进行token验证
//            if (!url.equals("/user/login") && !url.equals("/user/getOpenId") && !url.equals("/goodsCategories/categoryList") && !url.equals("/goods/goodsListByCategoryId") && !url.equals("/goods/goodsList")) {
//                validateToken(request);
//            }
//
//        //登录请求不需要验证token
//        doFilter(request, response, filterChain);
//    }
//
//
//    /**
//     * 验证token
//     *
//     * @param request
//     */
//    private void validateToken(HttpServletRequest request) {
//        //从头部获取token信息
//        String token = request.getHeader("Authorization");
//        //如果请求头部没有获取到token，则从请求的参数中进行获取
//        if (StringUtil.isNullOrEmpty(token)) {
//            token = request.getParameter("Authorization");
//        }
//        if (StringUtil.isNullOrEmpty(token)) {
//            throw new RuntimeException("token不存在");
//        }
//        //如果存在token，则从token中解析出用户名
//        User user=null;
//        String loginUserStr = null;
//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            Claims claims = JwtUtils.getClaimsFromToken(token);
//            loginUserStr = claims.getSubject();
//            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//            user = mapper.readValue(loginUserStr, User.class);
//
//        } catch (Exception e) {
//            throw new RuntimeException("token解析失败");
//        }
//        ThreadLocalUtil.setUser(user);
//    }
//
//}
